# -*- coding: utf-8 -*-
import jwt
from django.utils.deprecation import MiddlewareMixin
from jwt import ExpiredSignatureError
from rest_framework.authentication import get_authorization_header
from rest_framework import exceptions
from django.conf import settings
from django.contrib.auth import get_user_model
from django.http.response import JsonResponse
from rest_framework.status import HTTP_403_FORBIDDEN
from django.contrib.auth.models import AnonymousUser
from django.shortcuts import reverse
from django.conf import settings
OAUser = get_user_model()


class LoginCheckMiddleware(MiddlewareMixin):
    keyword = 'JWT'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.white_list = [reverse("oaauth:login"),'/staff/active/']

    def process_view(self, request, view_func, view_args, view_kwargs):
        if request.path in self.white_list or request.path.startswith(settings.MEDIA_URL):
            request.user = AnonymousUser()
            request.auth = None
            print("Middleware: User set to AnonymousUser")

            return None
        try:
            auth = get_authorization_header(request).split()
            if not auth or auth[0].lower() != self.keyword.lower().encode():
                raise exceptions.ValidationError('请传入JWT!')
            if len(auth) == 1:
                msg = '没有提供JWToken！'
                raise exceptions.AuthenticationFailed(msg)
            elif len(auth) > 2:
                msg = '无效的JWT！'
                raise exceptions.AuthenticationFailed(msg)

            try:
                jwt_token = auth[1]
                payload = jwt.decode(jwt_token, key=settings.SECRET_KEY, algorithms="HS256")
                userid = payload.get('userid')
                try:
                    user = OAUser.objects.get(pk=userid)
                    request.user = user
                    request.auth = jwt_token
                except Exception:
                    msg = '用户信息错误！'
                    raise exceptions.AuthenticationFailed(msg)
            except UnicodeError:
                msg = 'UnicodeError'
                raise exceptions.AuthenticationFailed(msg)
            except ExpiredSignatureError:
                msg = 'token已过期！'
                raise exceptions.AuthenticationFailed(msg)
        except Exception as e:
            print(e)
            return JsonResponse(data={"detail": "请登录！", }, status=HTTP_403_FORBIDDEN)
